package com.qjc.demo.data_structure.binary_tree;

/**
 * @ClassName: BianryTreeTest
 * @Description: 遍历二叉树
 * @Author: qiaojiacheng
 * @Date: 2021/6/29 6:23 下午
 */
public class BinaryTreeTest {

    public static void main(String[] args) {
        BinaryTreeNode binaryTreeNode1 = new BinaryTreeNode(1);
        BinaryTreeNode binaryTreeNode2 = new BinaryTreeNode(2);
        BinaryTreeNode binaryTreeNode3 = new BinaryTreeNode(3);

        BinaryTreeNode binaryTreeNode4 = new BinaryTreeNode(4);

        BinaryTreeNode binaryTreeNode5 = new BinaryTreeNode(5);
        BinaryTreeNode binaryTreeNode6 = new BinaryTreeNode(6);
        BinaryTreeNode binaryTreeNode7 = new BinaryTreeNode(7);

        binaryTreeNode4.leftNode = binaryTreeNode2;
        binaryTreeNode2.leftNode = binaryTreeNode1;
        binaryTreeNode2.rightNode = binaryTreeNode3;

        binaryTreeNode4.rightNode = binaryTreeNode6;
        binaryTreeNode6.leftNode = binaryTreeNode5;
        binaryTreeNode6.rightNode = binaryTreeNode7;

        /**
         *          4
         *        /   \
         *       2    6
         *     /  \  / \
         *    1   3 5  7
         */
        traverseBinaryTree(binaryTreeNode4);

    }

    private static void traverseBinaryTree(BinaryTreeNode head) {
        if (head != null) {
//            System.err.println(head.value);// 前序遍历：根节点->前序遍历左子树->前序遍历右子树（4，2，1，3，6，5，7）
            traverseBinaryTree(head.leftNode);
//            System.err.println(head.value);// 中序遍历：中序遍历左子树->根节点->中序遍历右子树（1，2，3，4，5，6，7）
            traverseBinaryTree(head.rightNode);
            System.err.println(head.value);// 后序遍历：后序遍历左子树->后序遍历右子树->根节点（1，3，2，5，7，6，4）
        }
    }

}
